<!--//This file is part of the store/retrieve project
//    (http://code.google.com/p/storeretrieve/).
//
//    Store/retrieve is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    Store/retrieve is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with store/retrieve. If not, see <http://www.gnu.org/licenses/>.
//-->


<%@ page contentType="text/html; charset=UTF-8" %>  
<html>

<%@ page import="java.util.Properties,
		java.util.ArrayList,
		java.io.File,
		java.util.HashMap,
		java.lang.StringBuilder,
		java.text.DecimalFormat,
		java.net.URLEncoder"
%>

<%@include file="includes/tools.jsp" %>
<%@include file="includes/backend.jsp" %>

<%! 	
	Properties props;
	File file;
	ArrayList<File> containerFiles;
	Document doc;
	String responseString = "";

	String getFileTable(String id) {
		StringBuilder o = new StringBuilder();
		o.append("<table border=\"1\" width=\"\" cellpadding=\"2\" cellspacing=\"2\">");
		long combinedSize = 0;
		for(File f : containerFiles) {
			combinedSize += f.length();
			o.append("<tr>");
			o.append("<td>" + getIconLink("x-office-document", 
				props.getProperty("EXTERNAL_DOCUMENT_VIEW_URL_TEMPLATE").replaceAll("\\*",  
				new File(props.getProperty("INTERNAL_REPO_PATH")).toURI().relativize(f.toURI()).getPath().replaceAll("/$", "")),
				"_self") + "</td>");
			try {
	                        o.append("<td>" + getIconLink("document-save", 
					"download.jsp?id=" + id + "&file=" + URLEncoder.encode(f.getName(), "UTF-8"),
                	                "_self") + "</td>");
			} catch(Throwable ex) {
                                 ex.printStackTrace();
                        } finally {} 

			o.append("<td>" + f.getName() + "</td>");
			o.append("<td>" + getFileSizeString(f.length()) + "</td>");
			o.append("</tr>");
		}
		o.append("<tr>");
		o.append("<td>");
		o.append(getIconLink("document-open", 
			props.getProperty("EXTERNAL_FOLDER_VIEW_URL_TEMPLATE").replaceAll("\\*",  
                        new File(props.getProperty("INTERNAL_REPO_PATH")).toURI().relativize(new File(file.getParent()).toURI()).getPath().replaceAll("/$", "")),
                        "_parent"));
		o.append("</td>");
		o.append("<td>");
	        o.append(getIconLink("edit-select-all",
                	("edit.jsp?id=" + id),
                        "_self"));
		o.append("</td>");
		o.append("<td>Total " + containerFiles.size() + " file(s)</td>");
		o.append("<td>" + getFileSizeString(combinedSize) + "</td>");
		o.append("</table>");
		return o.toString();
	}

	String getFileSizeString(Long size) {
		String result;
		double sizeMB = ((double)size/(1024*1024));
            	DecimalFormat formater = new DecimalFormat("#.##");
        	result = "" + Double.valueOf(formater.format(sizeMB));
		result = result + " MB";
		return result;
	}

%>

<%
	request.setCharacterEncoding("UTF-8");
	props = loadPropertiesByUser(request.getRemoteUser()); 
	
	if(props == null) {
		response.sendRedirect("login.jsp");
	} else {
		HashMap queryParms = new HashMap();
		queryParms.put("Storage/Id", request.getParameter("id"));
		ArrayList<File> matches = query(props.getProperty("INTERNAL_REPO_PATH"), queryParms);
		if(matches.size() == 1) {
			try {
				//Load XML
				file = new File(matches.get(0).getAbsolutePath() + "/meta.xml");
				DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
				DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
				doc = dBuilder.parse(file);
				doc.getDocumentElement().normalize();
			
				containerFiles = new ArrayList<File>();
				File[] container = matches.get(0).listFiles();
					for(File f : container) {
						if(f.isFile() && !(f.getName().equals("meta.xml")))  {
							containerFiles.add(f);
						}
					}

			} catch(Throwable ex) { 
				 ex.printStackTrace();
			} finally {}
		} else throw new Exception("No document found");
		responseString = getFileTable(request.getParameter("id"));
	}
%>

<head>
<title>store/retrieve view client</title>
<link rel="stylesheet" type="text/css" href="resources/<%= props.getProperty("CSS") %>" /> 
<link rel="icon" href="resources/logo-storeretrieve-favicon.png" type="image/png" />
</head>
<body>
<%= getLogoHtml() %>
<%= responseString %>
</body>
</html>

